log using partyleaderslog

*Preparing data set for analyses
clear
use "CVP_1_117.dta"
keep chamber congress icpsr party cvp_gls_adjusted
g house = chamber == "House"
keep if party == 100 | party == 200
g dem = party == 100
drop party chamber
rename cvp cvp
merge m:1 icpsr dem house using "HouseSenateLeaders.dta"
preserve
*extract leader ideologies
keep if _merge == 3
drop _merge
drop if congress >= mincong
collapse (mean) cvp house dem, by(icpsr)
merge 1:1 icpsr house dem using "HouseSenateLeaders.dta"
drop _merge
rename mincong cong0
forvalues i = 1/15 {
g cong`i' = cong0 + `i' if cong0 + `i' <= maxcong 
}
drop maxcong
reshape long cong, i(icpsr) j(num)
drop if cong == .
drop num icpsr 
sort house dem cong 
rename cvp leader_cvp
g leader_next = leader_cvp[_n+1] if house == house[_n+1] & dem == dem[_n+1]
g leader_prev = leader_cvp[_n-1] if house == house[_n-1] & dem == dem[_n-1]
rename cong congress
save "LeaderCVP_HouseSenate.dta", replace
restore
drop if _merge == 3 & congress >= mincong
drop _merge mincong maxcong
/*
classify members as moderate:
-compute first adjusted CVP for each member-party-chamber
-compute median first adjusted CVP for each congress-party-chamber
-if Dem is more conservative or Rep is more liberal than median, classify as a moderate member-Congress
-compute share of moderate Congresses for each member
-classify member as moderate if they are moderate for more than half of their congresses
*/
egen member = group(icpsr dem house)
egen mincong = min(congress), by(member)
g temp = cvp if congress == mincong
egen firstcvp = mean(temp), by(member)
drop temp mincong
keep if congress >= 76 & congress <= 117
egen cong_chamber_party = group(congress house dem)
egen medianfirst = median(firstcvp), by(cong_chamber_party)
g moderate = 0
replace moderate = 1 if firstcvp > medianfirst & dem == 1
replace moderate = 1 if firstcvp < medianfirst & dem == 0
egen meanmoderate = mean(moderate), by(member)
drop moderate
g moderate = meanmoderate > .5
drop member firstcvp cong_chamber_party medianfirst meanmoderate
*merge in leader CVP
merge m:1 congress house dem using "LeaderCVP_HouseSenate.dta"
drop _merge
save "Member_CVP_Cleaned.dta", replace

clear
import delimited "HSall_members.csv"
drop if chamber == "President"
g house = chamber == "House"
keep if congress >= 76 & congress <= 117
keep if party == 100 | party == 200
g dem = party == 100
keep congress house icpsr bioname dem
merge 1:1 congress house icpsr dem using "Member_CVP_Cleaned.dta"
keep if _merge == 3
drop _merge
g majority = dem
replace majority = 1 - dem if congress == 81 & house == 1
replace majority = 1 - dem if congress == 83 & house == 1
replace majority = 1 - dem if congress >= 104 & congress <= 109 & house == 1
replace majority = 1 - dem if congress >= 112 & congress <= 115 & house == 1
replace majority = 1 - dem if congress == 81 & house == 0
replace majority = 1 - dem if congress == 83 & house == 0
replace majority = 1 - dem if congress >= 104 & congress <= 109 & house == 0
replace majority = 1 - dem if congress >= 112 & congress <= 115 & house == 0
save "members_cleaned.dta", replace

clear
import delimited "HSall_votes.csv"
keep if congress >= 76 & congress <= 117
drop if cast_code == 0 | cast_code == 7 | cast_code == 8 | cast_code == 9
g yea = cast_code <= 3
g house = chamber == "House" 
drop cast_code prob
merge m:1 icpsr congress house using "members_cleaned.dta"
keep if _merge == 3
drop _merge
egen member_party_chamber = group(icpsr dem chamber)
egen party_chamber = group(dem chamber)
egen party_chamber_congress = group(dem chamber congress)
egen bill = group(congress chamber rollnumber)
g demyea = yea if dem == 1
g repyea = yea if dem == 0
egen meandemyea = mean(demyea), by(bill)
egen meanrepyea = mean(repyea), by(bill)
*drop unanimous votes
drop if meandemyea == 1 & meanrepyea == 1
drop if meandemyea == 0 & meanrepyea == 0
drop bill
egen bill = group(congress chamber rollnumber)
g conservative = yea
replace conservative = 1 - yea if meandemyea > meanrepyea
g withrepublicans = yea
replace withrepublicans = 1 - yea if meanrepyea < .5
g againstdemocrats = yea
replace againstdemocrats = 1 - yea if meandemyea > .5
drop *demyea *repyea chamber
sort congress house bill icpsr 
order congress house rollnumber icpsr dem majority bioname yea conservative withrepublicans againstdemocrats bill member_party_chamber party_chamber party_chamber_congress 
save "rollcalls_prelim.dta", replace
keep bill conservative cvp
save "votes_prelim.dta", replace

clear
postutil clear
postfile Recodings bill recode using "Recodings.dta", replace
use "votes_prelim.dta"
sum bill
local nbills = r(max)
quietly {
	forvalues i = 1/`nbills' {
	clear
	use "votes_prelim.dta" if bill == `i'
	reg conservative cvp
	if _b[cvp] < 0 {
		post Recodings (`i') (1)
		}
	if `i'/500 == round(`i'/500) {
		noisily: disp `i' " $S_TIME"
		}
	}
}
postclose Recodings
clear 
use "Recodings.dta"
sum recode
disp r(N) " " `nbills' " " r(max)/`nbills'

*update leader CVP for Senate Republicans in 91st Congress
clear
import delimited "S091_rollcalls.csv"
keep rollnumber date
g day = date(date, "YMD")
sort day 
*Dirksen died on September 7, 1969 (day 3537)
*Scott elected minority leader on September 24, 1969 (day 3554)
g noleader = day >= 3537 & day < 3554
g scott = day >= 3554
g congress = 91
g house = 0
drop day date
save "billdates.dta", replace

clear
use "rollcalls_prelim.dta"
merge m:1 bill using "Recodings.dta"
g reppartyvote = conservative
replace conservative = 1 - conservative if recode == 1
drop recode _merge
merge m:1 congress house rollnumber using "billdates.dta"
drop if _merge == 2
drop _merge
replace noleader = 0 if dem == 1 & house == 0 & congress == 91
replace scott = 0 if dem == 1 & house == 0 & congress == 91
replace leader_cvp = . if noleader == 1
sum leader_cvp if dem == 0 & house == 0 & congress == 90
replace leader_cvp = r(mean) if dem == 0 & house == 0 & congress == 91 & scott + noleader == 0
order congress house rollnumber icpsr dem majority bioname yea conservative reppartyvote withrepublicans againstdemocrats bill member_party_chamber party_chamber party_chamber_congress 
save "CleanedRollCallData.dta", replace


*Figure 1
clear
use "CleanedRollCallData.dta"
egen member_party_chamber_congress = group(icpsr dem house congress)
sort member_party_chamber_congress
drop if member_party_chamber_congress == member_party_chamber_congress[_n-1]
drop if rollnumber == .
collapse (mean) cvp leader_cvp dem house congress, by(party_chamber_congress)
drop party_chamber_congress
egen chamber_congress = group(house congress)
reshape wide cvp leader_cvp, i(chamber_congress) j(dem)
g eyear = 1786 + cong*2
g year = eyear + 1.5
g reppre = cvp0 if cong <= 92
g reppost = cvp0 if cong >= 92
g dempre = cvp1 if cong <= 92
g dempost = cvp1 if cong >= 92
graph twoway (line cvp0 cvp1 year) (scatter leader_cvp0 leader_cvp1 year) (lfit reppre year) (lfit reppost year) (lfit dempre year) (lfit dempost year) if house == 1
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush CVP
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Year
gr_edit .xaxis1.plotregion.xscale.curmax = 2021
gr_edit .plotregion1.plot1.style.editstyle line(color(red) width(thick)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(midblue) width(thick)) editcopy
gr_edit .plotregion1.plot3.style.editstyle marker(size(small) fillcolor(maroon) linestyle(color(maroon))) editcopy
gr_edit .plotregion1.plot4.style.editstyle marker(size(small) fillcolor(navy) linestyle(color(navy))) editcopy
gr_edit .plotregion1.plot5.style.editstyle line(color(cranberry) pattern(dash)) editcopy
gr_edit .plotregion1.plot6.style.editstyle line(color(cranberry) pattern(dash)) editcopy
gr_edit .plotregion1.plot7.style.editstyle line(color(blue) pattern(dash)) editcopy
gr_edit .plotregion1.plot8.style.editstyle line(color(blue) pattern(dash)) editcopy
gr_edit .title.style.editstyle color(black) editcopy
gr_edit .title.text = {}
gr_edit .title.text.Arrpush House
graph save "PolarizationHouse.gph", replace
graph twoway (line cvp0 cvp1 year) (scatter leader_cvp0 leader_cvp1 year) (lfit reppre year) (lfit reppost year) (lfit dempre year) (lfit dempost year) if house == 0
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush CVP
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Year
gr_edit .xaxis1.plotregion.xscale.curmax = 2021
gr_edit .plotregion1.plot1.style.editstyle line(color(red) width(thick)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(midblue) width(thick)) editcopy
gr_edit .plotregion1.plot3.style.editstyle marker(size(small) fillcolor(maroon) linestyle(color(maroon))) editcopy
gr_edit .plotregion1.plot4.style.editstyle marker(size(small) fillcolor(navy) linestyle(color(navy))) editcopy
gr_edit .plotregion1.plot5.style.editstyle line(color(cranberry) pattern(dash)) editcopy
gr_edit .plotregion1.plot6.style.editstyle line(color(cranberry) pattern(dash)) editcopy
gr_edit .plotregion1.plot7.style.editstyle line(color(blue) pattern(dash)) editcopy
gr_edit .plotregion1.plot8.style.editstyle line(color(blue) pattern(dash)) editcopy
gr_edit .title.style.editstyle color(black) editcopy
gr_edit .title.text = {}
gr_edit .title.text.Arrpush Senate
graph save "PolarizationSenate.gph", replace
graph combine "PolarizationHouse.gph" "PolarizationSenate.gph", col(1)
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .style.editstyle declared_ysize(8) editcopy
graph export "Polarization.png", replace as(png)


*Table 1
clear
use "CleanedRollCallData.dta"
keep if congress >= 77 & congress <= 116
*correlations reported in footnote
corr conservative reppartyvote withrepublicans againstdemocrats
g leader_cvp_dem = leader_cvp*dem
g leader_cvp_majority = leader_cvp*majority
g leader_cvp_house = leader_cvp*house
g leader_cvp_moderate = leader_cvp*moderate

areg conservative leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", replace 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg conservative leader_next leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg conservative leader_next leader_cvp leader_prev, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg conservative leader_cvp leader_cvp_house, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg conservative leader_cvp leader_cvp_dem, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg conservative leader_cvp leader_cvp_majority, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg conservative leader_cvp leader_cvp_moderate, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_conservative.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)

areg reppartyvote leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", replace 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg reppartyvote leader_next leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg reppartyvote leader_next leader_cvp leader_prev, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg reppartyvote leader_cvp leader_cvp_house, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg reppartyvote leader_cvp leader_cvp_dem, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg reppartyvote leader_cvp leader_cvp_majority, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg reppartyvote leader_cvp leader_cvp_moderate, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_republicandirection.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)

areg withrepublicans leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", replace 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg withrepublicans leader_next leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg withrepublicans leader_next leader_cvp leader_prev, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg withrepublicans leader_cvp leader_cvp_house, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg withrepublicans leader_cvp leader_cvp_dem, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg withrepublicans leader_cvp leader_cvp_majority, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg withrepublicans leader_cvp leader_cvp_moderate, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_withrepublicans.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)

areg againstdemocrats leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", replace 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg againstdemocrats leader_next leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg againstdemocrats leader_next leader_cvp leader_prev, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg againstdemocrats leader_cvp leader_cvp_house, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg againstdemocrats leader_cvp leader_cvp_dem, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg againstdemocrats leader_cvp leader_cvp_majority, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)
areg againstdemocrats leader_cvp leader_cvp_moderate, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table1_againstdemocrats.txt", append 2aster dec(3) sortvar(leader_next leader_cvp leader_prev)


*Table 2
clear
use "CleanedRollCallData.dta"
keep if house == 1
*focus on Congresses 87-91, where the only leader change in either party was from Halleck to Ford
keep if congress >= 87 & congress <= 91
*keep only members who served under both Halleck and Ford
egen mincong = min(congress), by(member_party_chamber)
egen maxcong = max(congress), by(member_party_chamber)
keep if mincong <= 88 & maxcong >= 89
merge m:1 icpsr using "fordvotes.dta"
replace fordvote1 = 0 if fordvote1 == .
replace fordvote2 = 0 if fordvote2 == .
g fordleadership = dem == 0 & congress >= 89
g ford_ford1 = fordleadership*fordvote1
g ford_ford2 = fordleadership*fordvote2

areg conservative fordleadership, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_conservative.txt", replace 2aster dec(3)
areg conservative fordleadership ford_ford1, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_conservative.txt", append 2aster dec(3)
areg conservative fordleadership ford_ford2, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_conservative.txt", append 2aster dec(3)
areg conservative fordleadership if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_conservative.txt", append 2aster dec(3)
areg conservative fordleadership ford_ford1 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_conservative.txt", append 2aster dec(3)
areg conservative fordleadership ford_ford2 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_conservative.txt", append 2aster dec(3)

areg reppartyvote fordleadership, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_republicandirection.txt", replace 2aster dec(3)
areg reppartyvote fordleadership ford_ford1, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_republicandirection.txt", append 2aster dec(3)
areg reppartyvote fordleadership ford_ford2, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_republicandirection.txt", append 2aster dec(3)
areg reppartyvote fordleadership if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_republicandirection.txt", append 2aster dec(3)
areg reppartyvote fordleadership ford_ford1 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_republicandirection.txt", append 2aster dec(3)
areg reppartyvote fordleadership ford_ford2 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_republicandirection.txt", append 2aster dec(3)

areg withrepublicans fordleadership, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_withrepublicans.txt", replace 2aster dec(3)
areg withrepublicans fordleadership ford_ford1, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_withrepublicans.txt", append 2aster dec(3)
areg withrepublicans fordleadership ford_ford2, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_withrepublicans.txt", append 2aster dec(3)
areg withrepublicans fordleadership if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_withrepublicans.txt", append 2aster dec(3)
areg withrepublicans fordleadership ford_ford1 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_withrepublicans.txt", append 2aster dec(3)
areg withrepublicans fordleadership ford_ford2 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_withrepublicans.txt", append 2aster dec(3)

areg againstdemocrats fordleadership, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_againstdemocrats.txt", replace 2aster dec(3)
areg againstdemocrats fordleadership ford_ford1, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_againstdemocrats.txt", append 2aster dec(3)
areg againstdemocrats fordleadership ford_ford2, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_againstdemocrats.txt", append 2aster dec(3)
areg againstdemocrats fordleadership if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_againstdemocrats.txt", append 2aster dec(3)
areg againstdemocrats fordleadership ford_ford1 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_againstdemocrats.txt", append 2aster dec(3)
areg againstdemocrats fordleadership ford_ford2 if congress >= 88 & congress <= 89, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "Table2_againstdemocrats.txt", append 2aster dec(3)


*Table 3
clear
use "CleanedRollCallData.dta"
keep if house == 0
keep if congress == 91
areg conservative noleader scott, a(member_party_chamber bill) vce(cluster member_party_chamber bill)
outreg2 using "Table3.txt", replace 2aster dec(3)
areg reppartyvote noleader scott, a(member_party_chamber bill) vce(cluster member_party_chamber bill)
outreg2 using "Table3.txt", append 2aster dec(3)
areg withrepublicans noleader scott, a(member_party_chamber bill) vce(cluster member_party_chamber bill)
outreg2 using "Table3.txt", append 2aster dec(3)
areg againstdemocrats noleader scott, a(member_party_chamber bill) vce(cluster member_party_chamber bill)
outreg2 using "Table3.txt", append 2aster dec(3)


*Appendix Figure A1
clear
postutil clear
use "CVP_1_117.dta"
keep chamber congress icpsr party cvp_gls_adjusted
g house = chamber == "House"
keep if party == 100 | party == 200
g dem = party == 100
drop party chamber
rename cvp cvp
merge m:1 icpsr dem house using "HouseSenateLeaders.dta"
keep if _merge == 3
drop _merge
drop if congress >= mincong
preserve
collapse (mean) cvp house dem, by(icpsr)
merge 1:1 icpsr house dem using "HouseSenateLeaders.dta"
drop _merge
rename mincong cong0
forvalues i = 1/15 {
g cong`i' = cong0 + `i' if cong0 + `i' <= maxcong 
}
drop maxcong
reshape long cong, i(icpsr) j(num)
drop if cong == .
drop num
rename icpsr leader_icpsr
sort house dem cong 
rename cvp leader_cvp
rename cong congress
save "Leader_CVP_ICPSR.dta", replace
keep if congress >= 77 & congress <= 116
keep leader_*
collapse leader_cvp, by(leader_icpsr)
save "Leader_CVP_temp.dta", replace
keep if leader_icpsr == 437 | leader_icpsr == 6367 | leader_icpsr == 7753 | leader_icpsr == 6033 
save "firstleaders.dta", replace
restore
drop mincong maxcong
merge m:1 congress house dem using "Leader_CVP_ICPSR.dta"
keep if _merge == 3
egen party_chamber = group(dem house)
g temp = leader_cvp if congress == 77
egen firstleader_cvp = mean(temp), by(party_chamber)
drop temp party_chamber
drop _merge leader_cvp
save "futureleaders.dta", replace
clear
use "CleanedRollCallData.dta"
keep if congress >= 77 & congress <= 116
drop majority bioname yea reppartyvote withrepublicans againstdemocrats cvp moderate leader_next leader_prev leader_cvp
merge m:1 house dem congress using "Leader_CVP_ICPSR.dta"
drop if noleader == 1
sum leader_icpsr if dem == 0 & house == 0 & congress == 90
replace leader_icpsr = r(mean) if dem == 0 & house == 0 & congress == 91 & scott + noleader == 0
drop _merge noleader scott leader_cvp
save "CleanedDataForIterative.dta", replace

clear
postutil clear
postfile Results coef sterr pval iteration using "Results.dta", replace
scalar leadereffect = 0
forvalues i = 1/16 {
quietly {
clear
use "futureleaders.dta"
merge m:1 leader_icpsr using "Leader_CVP_temp.dta"
keep if _merge == 3
g cvp_corrected = cvp - (leader_cvp - firstleader_cvp)*leadereffect
collapse cvp_corrected, by(icpsr)
rename cvp_corrected leader_cvp
rename icpsr leader_icpsr
append using "firstleaders.dta"
save "Leader_CVP_temp.dta", replace
clear
use "CleanedDataForIterative.dta"
merge m:1 leader_icpsr using "Leader_CVP_temp"
drop _merge
areg conservative leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
scalar leadereffect = _b[leader_cvp]
post Results (_b[leader_cvp]) (_se[leader_cvp]) (2*ttail(e(df_r),abs(_b[leader_cvp]/_se[leader_cvp]))) (`i')
}
disp `i' " $S_TIME " leadereffect
}
postclose Results
clear
use "Results.dta"
g upper = coef + sterr*1.96
g lower = coef - sterr*1.96
graph twoway line upper lower coef iteration, yline(0)
gr_edit .plotregion1._xylines[1].style.editstyle linestyle(color(gs8) width(medthin)) editcopy
gr_edit .legend.draw_view.setstyle, style(no)
gr_edit .xaxis1.title.text = {}
gr_edit .xaxis1.title.text.Arrpush Iteration
gr_edit .yaxis1.title.text = {}
gr_edit .yaxis1.title.text.Arrpush Estimated Leader Effect
gr_edit .xaxis1.reset_rule 1 16 3, tickset(major) ruletype(range) 
gr_edit .yaxis1.reset_rule 0 .6 .2, tickset(major) ruletype(range) 
gr_edit .yaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .xaxis1.style.editstyle majorstyle(gridstyle(linestyle(color(white)))) editcopy
gr_edit .style.editstyle margin(vsmall) boxstyle(shadestyle(color(white)) linestyle(color(white))) editcopy
gr_edit .plotregion1.plot1.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot2.style.editstyle line(color(black) width(medthin) pattern(dash)) editcopy
gr_edit .plotregion1.plot3.style.editstyle line(color(black) width(thick)) editcopy
graph export "Iterative.png", replace as(png)


*Appendix Table 1
clear
use "SenateAllLeaders.dta"
g whip0 = whip_start
forvalues i = 1/11 {
g whip`i' = whip0 + `i' if whip0 + `i' <= whip_end 
}
g leader0 = leader_start
forvalues i = 1/11 {
g leader`i' = leader0 + `i' if leader0 + `i' <= leader_end 
}
drop whip_* leader_*
reshape long whip leader, i(icpsr) j(num)
drop if whip == . & leader == .
drop num
preserve
drop leader
drop if whip == .
rename whip congress
g whip = 1
save "whip.dta", replace
restore
drop whip
drop if leader == .
rename leader congress
g leader = 1
append using "whip.dta"
replace leader = 0 if leader == .
replace whip = 0 if whip == .
save "SenateAllLeadersCleaned.dta", replace
egen firstcongasleader = min(congress), by(icpsr)
keep icpsr name firstcong
sort icpsr
drop if icpsr == icpsr[_n-1]
save "SenateAllLeadersFirst.dta", replace
clear
use "CVP_1_117.dta"
rename name name2
keep chamber congress icpsr party cvp_gls_adjusted name2
keep if chamber == "Senate"
keep if party == 100 | party == 200
merge m:1 icpsr using "SenateAllLeadersFirst.dta"
keep if _merge == 3
drop _merge
*Kenneth Wherry was party whip in first Congress, so use his score from his first Congress
replace firstcongasleader = firstcongasleader + 1 if icpsr == 9998
drop if congress >= firstcongasleader
collapse cvp_gls_adjusted, by(icpsr)
rename cvp_gls_adjusted ideology
save "SenateAllLeadersIdeologies.dta", replace
clear
use "SenateAllLeadersCleaned.dta"
merge m:1 icpsr using "SenateAllLeadersIdeologies.dta"
drop _merge
*no Republican Senate whip in 77th congress, just focus on 78th through 117th
keep if congress >= 78 & congress <= 117
egen party_cong = group(dem congress)
g leader_cvp = ideology if leader == 1
g otherleader_cvp = ideology if whip == 1
collapse leader_cvp otherleader_cvp dem congress, by(party_cong)
drop party_cong
save "SenateAllLeaderIdeologiesByPartyCongress.dta", replace
clear
use "CleanedRollCallData.dta"
keep if house == 0
merge m:1 icpsr congress using "SenateAllLeadersCleaned.dta" 
keep if _merge == 1
drop _merge
drop leader_*
merge m:1 dem congress using "SenateAllLeaderIdeologiesByPartyCongress.dta"
tab congress _merge
keep if _merge == 3
drop _merge
keep congress house rollnumber icpsr dem conservative bill-party_chamber_congress leader_cvp otherleader_cvp
save "CleanedRollCallData_SenateAllLeaders.dta", replace
clear
use "HouseAllLeaders.dta"
reshape long leader whip ml, i(icpsr) j(num)
drop if whip == . & leader == . & ml == .
drop num
preserve
drop leader ml
drop if whip == .
rename whip congress
g whip = 1
save "whip.dta", replace
restore
preserve
drop whip leader
drop if ml == .
rename ml congress
g ml = 1
save "ml.dta", replace
restore
drop whip ml
drop if leader == .
rename leader congress
g leader = 1
append using "whip.dta"
append using "ml.dta" 
sort icpsr congress
replace leader = 0 if leader == .
g otherleader = whip == 1 | ml == 1
drop whip ml
save "HouseAllLeadersCleaned.dta", replace
egen firstcongasleader = min(congress), by(icpsr)
keep icpsr name firstcong
sort icpsr
drop if icpsr == icpsr[_n-1]
save "HouseAllLeadersFirst.dta", replace
clear
use "CVP_1_117.dta"
rename name name2
keep chamber congress icpsr party cvp_gls_adjusted name2
keep if chamber == "House"
keep if party == 100 | party == 200
merge m:1 icpsr using "HouseAllLeadersFirst.dta"
keep if _merge == 3
drop _merge
drop if congress >= firstcongasleader
collapse cvp_gls_adjusted, by(icpsr)
rename cvp_gls_adjusted ideology
save "HouseAllLeadersIdeologies.dta", replace
clear
use "HouseAllLeadersCleaned.dta"
merge m:1 icpsr using "HouseAllLeadersIdeologies.dta"
drop _merge
keep if congress >= 77 & congress <= 117
egen party_cong = group(dem congress)
g leader_cvp = ideology if leader == 1
g otherleader_cvp = ideology if otherleader == 1
collapse leader_cvp otherleader_cvp dem congress, by(party_cong)
drop party_cong
save "HouseAllLeaderIdeologiesByPartyCongress.dta", replace
clear
use "CleanedRollCallData.dta"
keep if house == 1
merge m:1 icpsr congress using "HouseAllLeadersCleaned.dta" 
keep if _merge == 1
drop _merge
drop leader_*
merge m:1 dem congress using "HouseAllLeaderIdeologiesByPartyCongress.dta"
tab congress _merge
keep if _merge == 3
drop _merge
keep congress house rollnumber icpsr dem conservative bill-party_chamber_congress leader_cvp otherleader_cvp
append using "CleanedRollCallData_SenateAllLeaders.dta"
keep if congress >= 78 & congress <= 116
areg conservative leader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "OtherLeaders.txt", replace 2aster dec(3) sortvar(leader_cvp otherleader_cvp)
areg conservative otherleader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "OtherLeaders.txt", append 2aster dec(3) sortvar(leader_cvp otherleader_cvp)
areg conservative leader_cvp otherleader_cvp, a(member_party_chamber bill) vce(cluster member_party_chamber party_chamber_congress)
outreg2 using "OtherLeaders.txt", append 2aster dec(3) sortvar(leader_cvp otherleader_cvp)


log close
translate partyleaderslog.smcl partyleaderslog.pdf



